package com.weigee.ktv.v.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.l.d.app.library.util.ResourceUtil;
import com.l.d.app.library.view.FocusedTextView;
import com.weigee.ktv.R;

import butterknife.ButterKnife;

/**
 * Created by 林德 on 2017/4/5 16:34.
 * <p/>
 */
public class WrapTitleBar extends RelativeLayout {
    /**
     * 左边按钮（group）
     */
    private FrameLayout m_frameLeft;
    /**
     * 左边按钮
     */
    private SimpleCustomButton m_btnLeft;
    /**
     * 标题
     */
    private FocusedTextView m_tvTitle;
    // 右边标题
    private FocusedTextView m_tvTitleRight;

    public WrapTitleBar(Context context) {
        this(context, null);
    }

    public WrapTitleBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WrapTitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.wrap_title_bar, this, true);
        m_frameLeft = ButterKnife.findById(this, R.id.frame_left_btn);
        m_btnLeft = ButterKnife.findById(this, R.id.simple_cus_button);
        m_tvTitle = ButterKnife.findById(this, R.id.tv_title);
        m_tvTitleRight = ButterKnife.findById(this, R.id.tv_title_right);

        m_tvTitleRight.setVisibility(GONE);
    }

    public void setTitle(CharSequence title) {
        if (m_tvTitle == null) return;
        m_tvTitle.setText(title);
    }

    /**
     * @see SimpleCustomButton attrs
     */
    public void setLeftButtonType(int type) {
        if (m_btnLeft == null) return;
        final Context context = getContext();
        if (context == null) return;
        final ViewGroup.LayoutParams lp = m_btnLeft.getLayoutParams();
        final float width;
        final float height;
        switch (type) {
            case 1:
            case 2:
            case 3:
            case 4:
            default:
                width = ResourceUtil.dimension(context, R.dimen.dimen12dp);
                height = ResourceUtil.dimension(context, R.dimen.dimen20dp);
                break;
            case 5:
                width = ResourceUtil.dimension(context, R.dimen.dimen25dp);
                height = ResourceUtil.dimension(context, R.dimen.dimen20dp);
                break;
        }
        lp.width = (int) width;
        lp.height = (int) height;
        m_btnLeft.setLayoutParams(lp);

        m_btnLeft.setButtonType(type);
    }

    @Deprecated
    public void setLeftButtonListener(View.OnClickListener listener) {
        if (m_frameLeft == null) return;
        m_frameLeft.setOnClickListener(listener);
    }

    @Deprecated
    public void setLeftButtonLongListener(View.OnLongClickListener listener) {
        if (m_frameLeft == null) return;
        m_frameLeft.setOnLongClickListener(listener);
    }

    @Deprecated
    public void setLeftButtonVisibility(int visibility) {
        if (m_frameLeft == null) return;
        m_frameLeft.setVisibility(visibility);
    }

    public void setLeftButton(int visibility, View.OnClickListener listener) {
        if (m_frameLeft == null) {
            return;
        }
        if (visibility == VISIBLE || visibility == INVISIBLE || visibility == GONE) {
            m_frameLeft.setVisibility(visibility);
        }
        m_frameLeft.setOnClickListener(listener);
    }

    public void setTitleRight(int visibility, CharSequence text, View.OnClickListener listener) {
        setTextViewParams(m_tvTitleRight, visibility, text, listener);
    }

    private void setTextViewParams(TextView view, int visibility, CharSequence text, View.OnClickListener listener) {
        if (view == null) {
            return;
        }
        if (visibility == VISIBLE || visibility == INVISIBLE || visibility == GONE) {
            view.setVisibility(visibility);
        }
        view.setText(text);
        view.setOnClickListener(listener);
    }
}
